package com.example.application.views.petStore;

import com.example.application.bean.*;
import com.example.application.views.main.MainView;
import com.example.application.views.util.ShowAllInfosUtil;
import com.example.application.views.util.DialogUtil;
import com.vaadin.flow.component.UI;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.dependency.CssImport;
import com.vaadin.flow.component.gridpro.GridPro;
import com.vaadin.flow.component.html.Div;
import com.vaadin.flow.component.orderedlayout.FlexComponent;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.router.PageTitle;
import com.vaadin.flow.router.Route;

import java.util.ArrayList;
import java.util.List;

@Route(value = "petStore", layout = MainView.class)
@PageTitle("门店")
@CssImport("./views/pet_store/petstore-view.css")
public class PetStoreView extends Div {

    public PetStoreView() {
        User user = (User) UI.getCurrent().getSession().getAttribute("user");
        if (user == null){

        }else if("门店管理员".equals(user.getIdentity())){
            this.init();
        }else{
            DialogUtil.adminAuthority();
        }
    }

    private Button createAddButton(GridPro<SupGoods> grid, SupGoods supGoods) {
        @SuppressWarnings("unchecked")
        Button  button = new Button("采购", clickEvent -> {

        });

        return button;
    }

    public void init(){
        HorizontalLayout horizontalLayout = new HorizontalLayout();
        HorizontalLayout layout = new HorizontalLayout();

        Button stoGoods = new Button("查看商品");
        Button service = new Button("查看服务");
        Button supGoods = new Button("查看供应商品");

        stoGoods.addClickListener((e)->{
            layout.removeAll();
            GridPro<StoGoods> stoGoodsGrid = new GridPro<>();

            stoGoodsGrid.addColumn(StoGoods::getName).setHeader("商品名");
            stoGoodsGrid.addColumn(StoGoods::getType).setHeader("类型");
            stoGoodsGrid.addColumn(StoGoods::getMaterial).setHeader("材质");
            stoGoodsGrid.addColumn(StoGoods::getApplicable).setHeader("适用规格");
            stoGoodsGrid.addColumn(StoGoods::getExclusive).setHeader("专属规格");
            stoGoodsGrid.addColumn(StoGoods::getPackingSize).setHeader("包装规格");
            stoGoodsGrid.addColumn(StoGoods::getFlavor).setHeader("口味");
            stoGoodsGrid.addColumn(StoGoods::getFunction).setHeader("特殊功用");
            stoGoodsGrid.addColumn(StoGoods::getOrigin).setHeader("产地");
            stoGoodsGrid.addColumn(StoGoods::getDateOfPro).setHeader("生产日期");
            stoGoodsGrid.addColumn(StoGoods::getExpDate).setHeader("保质期");
            stoGoodsGrid.addColumn(StoGoods::getSupId).setHeader("供货商ID");
            stoGoodsGrid.addColumn(StoGoods::getExplain).setHeader("特色说明");
            stoGoodsGrid.addEditColumn(StoGoods::getPrice).text((item, newValue)->{
                item.setPrice(Double.parseDouble(newValue));
            });
            stoGoodsGrid.addColumn(StoGoods::getPic).setHeader("图片");

            ResultBean<PageBean> resultBean = ShowAllInfosUtil.showInfos("http://localhost:8081/pet/stoGoods/showAllStoGoods", 1, 3, StoGoods.class);

            stoGoodsGrid.setItems(resultBean.getT().getDatas());

            layout.add(stoGoodsGrid);

        });

        service.addClickListener((e)->{
            layout.removeAll();
            GridPro<Service> serviceGrid = new GridPro<>();

            serviceGrid.addColumn(Service::getName).setHeader("服务名");
            serviceGrid.addColumn(Service::getType).setHeader("类型");
            serviceGrid.addEditColumn(Service::getSchedule).text(Service::setSchedule).setHeader("排期");
            serviceGrid.addEditColumn(Service::getApplicable).text(Service::setApplicable).setHeader("适用规格");
            serviceGrid.addEditColumn(Service::getServiceSpec).text(Service::setServiceSpec).setHeader("服务规格");
            serviceGrid.addEditColumn(Service::getSpendTime).text(Service::setSpendTime).setHeader("耗时");
            serviceGrid.addEditColumn(Service::getClerkLevel).text(Service::setClerkLevel).setHeader("店员等级");
            serviceGrid.addEditColumn(Service::getPrice).text((item, newValue) ->{
                item.setPrice(Double.parseDouble(newValue));
            }).setHeader("价格");

            ResultBean resultBean = ShowAllInfosUtil.showInfos("", 1, 3, Service[].class);
            List<Service> serviceList = new ArrayList<>();
            for (Service value : (Service[]) resultBean.getT()) {
                serviceList.add(value);
            }

            serviceGrid.setItems(serviceList);

            layout.add(serviceGrid);
        });

        supGoods.addClickListener((e)->{
            layout.removeAll();
            GridPro<SupGoods> supGoodsGrid = new GridPro<>();

            supGoodsGrid.addColumn(SupGoods::getName).setHeader("商品名");
            supGoodsGrid.addColumn(SupGoods::getType).setHeader("类型");
            supGoodsGrid.addColumn(SupGoods::getMaterial).setHeader("材质");
            supGoodsGrid.addColumn(SupGoods::getApplicable).setHeader("适用规格");
            supGoodsGrid.addColumn(SupGoods::getExclusive).setHeader("专属规格");
            supGoodsGrid.addColumn(SupGoods::getPackingSize).setHeader("包装规格");
            supGoodsGrid.addColumn(SupGoods::getFlavor).setHeader("口味");
            supGoodsGrid.addColumn(SupGoods::getFunction).setHeader("特殊功用");
            supGoodsGrid.addColumn(SupGoods::getOrigin).setHeader("产地");
            supGoodsGrid.addColumn(SupGoods::getDateOfPro).setHeader("生产日期");
            supGoodsGrid.addColumn(SupGoods::getExpDate).setHeader("保质期");
            supGoodsGrid.addColumn(SupGoods::getSupId).setHeader("供货商ID");
            supGoodsGrid.addColumn(SupGoods::getExplain).setHeader("特色说明");
            supGoodsGrid.addEditColumn(SupGoods::getPrice).text((item, newValue)->{
                item.setPrice(Double.parseDouble(newValue));
            });
            supGoodsGrid.addColumn(SupGoods::getPic).setHeader("图片");

            supGoodsGrid.addComponentColumn(item -> createAddButton(supGoodsGrid, item)).setHeader("操作");

            //供货商信息也要写

            ResultBean resultBean = ShowAllInfosUtil.showInfos("", 1, 3, SupGoods[].class);
            List<SupGoods> grid = new ArrayList<>();
            for (SupGoods goods : (SupGoods[]) resultBean.getT()) {
                grid.add(goods);
            }

            supGoodsGrid.setItems(grid);

            layout.add(supGoodsGrid);
        });


        horizontalLayout.add(stoGoods, service, supGoods);
        horizontalLayout.setJustifyContentMode(FlexComponent.JustifyContentMode.START);
        this.add(horizontalLayout, layout);
    }

}